/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf;

import com.controllers.CompanyController;
import com.entities.Company;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import org.primefaces.component.message.Message;

/**
 *
 * @author ben
 */
@Named(value = "companyAction")
@RequestScoped
public class CompanyAction implements Serializable {

    /**
     * Creates a new instance of CompanyAction
     */
    private Company company;
    private List<Company> companies;
    private static final Logger logger = Logger.getLogger(CompanyDenominationAction.class.getName());
    @ManagedProperty(value = "creditView")
    CreditView creditView;

    public List<Company> getCompanies() {

        return companies = companyController.getAllCompanies();
    }
    @EJB
    private CompanyController companyController;
    private Message messages;

    public CompanyAction() {
        this.company = new Company();

    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public String saveCompany() {

        try {
            companyController.save(company);
            logger.info("Saving Complete");
            setCompany(new Company());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saving Company Successful", "Saving Company Successful"));
            //creditView.companyTab(0);
            return null;

        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Saving Company", "Error Saving Company "));
            //creditView.companyTab(0);
            e.printStackTrace();
            return null;
        }

    }

    public CreditView getCreditView() {
        return creditView;
    }

    public void setCreditView(CreditView creditView) {
        this.creditView = creditView;
    }

    public CompanyController getCompanyController() {
        return companyController;
    }

    public void setCompanyController(CompanyController companyController) {
        this.companyController = companyController;
    }

    public Message getMessages() {
        return messages;
    }

    public void setMessages(Message messages) {
        this.messages = messages;
    }
}
